package com.ate.ui;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.ate.javamvp.R;
import com.ate.bean.GoodsBean;
import com.ate.javamvp.mvp.contract.Contract;
import com.ate.javamvp.mvp.presenter.DataPresneter;
import com.ate.javamvp.mvp.presenter.LoginPresenter;
import com.ate.lib_core.http.mvp.view.BaseActity;
import com.blankj.utilcode.util.ToastUtils;
import com.google.gson.JsonObject;

import java.util.List;

import okhttp3.MediaType;
import okhttp3.RequestBody;

public class LoginActivity extends BaseActity<DataPresneter> implements Contract.DataXiaView {
    private Button autoLogin;
    private TextView otherLogin;
    private Context context;

    @Override
    public int bandlayout() {
        return R.layout.activity_login;
    }

    @Override
    public void initView() {
        autoLogin = (Button) findViewById(R.id.autoLogin);
        otherLogin = (TextView) findViewById(R.id.otherLogin);
        String json = "{\n" +
                "  \"id\": 1,\n" +
                "  \"username\": \"sample string 2\",\n" +
                "  \"pwd\": \"sample string 3\",\n" +
                "  \"sex\": \"sample string 4\",\n" +
                "  \"birthday\": \"sample string 5\"\n" +
                "}";
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), json);
        context = LoginActivity.this;
        autoLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new LoginPresenter(LoginActivity.this).login(requestBody);
            }
        });
    }

    @Override
    public void LoginSucce(JsonObject jsonObject) {
        ToastUtils.showShort("登陆成功");
        SharedPreferences sp = getSharedPreferences("login",MODE_PRIVATE);
        SharedPreferences.Editor edit = sp.edit();
        edit.putString("jsonobject", jsonObject.toString());
        edit.commit();
        Intent intent = new Intent(LoginActivity.this, MainActivity.class);
        startActivity(intent);
    }

    @Override
    public void initData() {

    }

    @Override
    public void onSuccess(List<GoodsBean.DataBean> data) {

    }

    @Override
    public void onFail() {

    }
}